Repository navigation
chore(agents): stop agents pushing code verify hasn't passed - #135
Merged
Merged
Conversation
This was referenced Nov 27, 2026
doistbot
reviewed
Nov 27, 2026
doistbot
left a comment
Member
There was a problem hiding this comment.
This PR replaces the end-of-turn gate with a PreToolUse push check: a shell/push command parser, a guard that verifies the pushed commit against the verify record, hook wiring, and docs/test coverage.
Few things worth tightening:
- The cheap pre-filter in both hook configs misses a
gh pr createsplit across lines with a trailing backslash ([[:space:]\\]doesn't match the escapedn), so that payload exits 0 and an unverified PR opens untouched — widen the character class and pin the continuation form with a wiring test. - Force-created destinations can slip through:
-C/-B(git switch -C copy && git push origin copy) are checked against the old destination tip, which passes the unchanged-from-main exemption even though the push lands the unverified HEAD — resolve names incheckout.createdtoheadfirst and add a regression test for an existing destination branch. - Explicit refspecs are discarded when the command carries inline
push.*/remote.*/branch.*config (git -c push.default=matching push origin mainreportssources: ['*']and is wrongly denied), so onlyextraArgsshould replace refspecs — and the guard tests should stubGIT_CONFIG_GLOBAL/GIT_CONFIG_NOSYSTEMlike push-check.test.ts does, since baregit pushbehavior depends on the developer's global config. - Guard robustness: suppress only disappearance errors (ENOENT) in the snapshot read catch and rethrow permission/I/O failures so dirty-tree denials aren't misleading; and don't take the working-tree shortcut for
git commit -am … && git push, since a partially staged commit holds less than the tree verify hashed — reserve the shortcut for commands that stage everything.
I also included a few optional follow-up notes in the details below.
Optional follow-up notes (9)
.agents/scripts/lib/push-targets.ts:91: A push reached through a leading word the reader doesn't recognize is invisible.
readCommandonly inspectswords[start], sonice git push,stdbuf -oL git push, or a command name from a variable —cmd="git push -u origin feature"; $cmd, or$(echo git) push— produce no target and the command is allowed, while$BRANCHin refspec position is deliberately treated as unknown. Consider treating an unrecognized first word that starts with$as unknown, and adding the common zero-setup wrappers to WRAPPERS..agents/scripts/guard.test.ts:86:
repo.dir.split('/').pop()hand-rolls whatbasename(repo.dir)already does (it's also the separator-safe API).push-check.test.ts:25uses the same expression..agents/scripts/lib/push-check.test.ts:24: This linked worktree is removed only at the end of the test body, so a failing assertion leaves it behind in
tmpdir()—TempRepo.cleanup()only removesdirandaddOrigin's folders.guard.test.tsbuilds the same worktree inline. PutaddWorktreeintest-repo.tsnext toaddOriginand track the path inextraDirs, so both tests get cleanup for free and the setup lives in one place..agents/scripts/guard.test.ts:30: Pass-through wrapper for
repo.commitOnBranchthat drops the optionalcontentargument and adds nothing. Callrepo.commitOnBranch(branch, path)directly in the tests and delete the alias..agents/scripts/hook-wiring.test.ts:68: The
diroption on therunhelper is never passed by any call site — every test runs inrepo.dir, and thebashpayload also hardcodescwdtorepo.dir/src. Drop the option and userepo.dirdirectly, or wire it up if a worktree case is coming..agents/scripts/guard.ts:54: The command is parsed twice whenever it contains a push or
gh pr create:findTargetsruns here to filter, thencheckCommand(push-check.ts line 65) callsfindTargets(command, cwd)again on the same string. Since the filter exists precisely to keep non-push commands cheap, the push path should pay the parse once too — e.g. pass the parsed targets intocheckCommandand let it skip its own re-parse..agents/scripts/lib/push-check.ts:81: Checkout resolution isn't cached per path, so a command with several pushes or PRs from the same checkout repeats git subprocesses per target:
checkoutOf(target.dir)(a rev-parse) andguarded(path)→commonDir(path)(a rev-parse) here, plusconfiguredPush(path, ...)(several git config calls) at line 104 for each implicit target — e.g.git push && git pushrunsconfiguredPushtwice against the same checkout. Thecheckoutsmap already keys onpath; memoizecheckoutOf/commonDirresults per path (andconfiguredPushper root) alongside it, the same wayopenCheckoutmemoizesdefaultRefandtree..agents/scripts/lib/push-check.test.ts:348:
addWorktreecreates the worktree besiderepo.dir, butrepo.cleanup()inafterEachonly removesrepo.dir(and origins). If any assertion above fails, thisworktree removeis skipped and the sibling temp directory leaks. Wrap the test body in try/finally, asguard.test.ts's worktree test already does..agents/scripts/lib/push-targets.test.ts:35: The expected value for
cd ~/codeis built the same way the code builds it:join(homedir(), 'code')mirrorsresolveDir'sjoin(homedir(), to.slice(2)), so the assertion can't tell expansion from a bug in how~is joined. StubHOMEin the test (vi.stubEnv('HOME', '/home/tester')) and expect the literal/home/tester/code.
Replaces the end-of-turn gate with a push check, ported from todoist/todoist-cli#672. Before each shell command, the Claude Code and Codex hooks (now one PreToolUse hook on Bash, replacing UserPromptSubmit and Stop) call .agents/scripts/guard.ts. It stops an agent's git push or gh pr create from this checkout until npm run verify has passed on exactly the commit being pushed, not the working tree. Pushes a person runs in their own terminal never go through it. The guard reads the shell command without running it (cd, git -C, wrappers, eval, sh -c, quotes and here-documents), fingerprints the pushed commit from git ls-tree, and lets through changes verify records nothing for, dry runs, deletions and tag-only pushes. A push whose contents it can't tell is stopped. DISABLE_AGENT_GUARD=1 still turns it off, a checkout without the guard is skipped, a guard that fails allows the action and says why, and the retired turn-start and turn-end events are ignored quietly.
An inline `git -c push.*`, `remote.*` or `branch.*` setting only decides what a push with no refspec sends: a refspec in the command wins over them, as in git. The push check treated any such setting like xargs and read the push as every branch, so `git -c push.default=matching push origin main` was stopped as "pushes every branch at once".
`git switch -C copy && git push origin copy` resets `copy` to what's checked out before it pushes, but the push check looked `copy` up as it was before the command ran. When `copy` held main's code, the push passed as needing no verify, though it sent the unverified HEAD. The same went for `git checkout -B` and `git branch -f`. The check now reads a branch the command resets as what it points it at: what's checked out, or the start point `git branch -f` names.
The working-tree snapshot left out any file it failed to read, as if it had been deleted, so a permission or I/O error could change the fingerprint without a word and turn into a misleading "commit what belongs in it" denial. It now leaves out only files that are gone (ENOENT, ENOTDIR) and throws on anything else, which the push check reports as a guard failure that allowed the action.
JSON writes a line continuation in the command as `\\\n`, so the text between `pr` and `create` holds backslashes and an `n`. The hook's filter still passes it on, since the `*` after its bracket expression is a glob that takes any text, not a repeat of the bracket. The test now runs `gh pr create`, `gh pr new` and `git push` split across lines through both hook commands.
The three hook runs for commands split across lines made the first wiring test take over vitest's five-second limit while the rest of `npm run verify` ran alongside it.
…mit takes For `git commit … && git push`, the push check compared the working tree with the verify record, as if the commit took all of it. A commit can take less: `git commit -am` leaves out untracked files, a plain commit leaves out unstaged changes, and named paths or `--patch` leave out the rest. Such a push went through though the commit held less than verify checked. The check now reads what the command stages and what the commit takes. It uses the working tree only when the commit holds all of it: everything staged first (`git add -A`, `git add .` from the top folder), `git commit -a` with no untracked files, or a plain commit with nothing unstaged. Otherwise it stops the push and says to stage everything in the same command, or to commit first and push on its own.
scottlovegrove
force-pushed
the
scottl/agent-push-check
branch
from
November 27, 2026 14:30
6c2daca to
916107c
Compare
2 tasks done
scottlovegrove
added a commit
to todoist/todoist-mcp
that referenced
this pull request
Nov 27, 2026
# Pull Request ## Short description Part 1 of 3. Replaces the end-of-turn gate with the push check from todoist/todoist-cli#672. Before each shell command, the Claude Code and Codex hooks (now one `PreToolUse` hook on `Bash`, replacing `UserPromptSubmit` and `Stop`) call `.agents/scripts/guard.ts`, which stops an agent's `git push` or `gh pr create` until `npm run verify` has passed on exactly the commit being pushed. It reads the shell command without running it, fingerprints the pushed commit from `git ls-tree` (so the working-tree snapshot now hashes files the way git hashes a blob), and lets through what verify records nothing for. Pushes a person runs in their own terminal never go through it. `DISABLE_AGENT_GUARD=1` still turns it off, and a guard that fails to run allows the action and says why. The guard ignores the retired `turn-start`/`turn-end` events that older configs still send. Where todoist-mcp's tooling differs from todoist-cli's, this keeps todoist-mcp's version: its verify steps, `ci.yml` as the one workflow a local test reads, and the vite build. Only the tree.ts and git.ts pieces the push check needs came across (blob-id hashing, deleted files left out of the snapshot, docs counted by name, `snapshotCommit`, `commitChanges`, `changeNeedsChecks`, `resolveCommit`, `findRepoRoot`, `branchBase` with a head). Verify now treats a deleted doc as code too, so it agrees with the push check. What counts as docs changes here. The format check (oxfmt) reads Markdown, JSON and YAML, so a README or workflow edit fails CI's format check without any code changing. Only `CHANGELOG.md` and `LICENSE` are docs now, and `.nvmrc` (read only by the release workflow) is the one CI-only file. A test in `tree.test.ts` makes every file classified that way badly formatted and checks oxfmt still passes. It carries the fixes Doistbot found when the same port went into comms-cli, outline-cli and zendesk-cli, each with a test that fails without it: - A commit and push in one command uses the working tree only when the commit takes all of it (comms-cli, todoist/comms-cli#135): everything staged first, `commit -a` with no untracked files, or a plain commit with nothing unstaged. `git commit -am x && git push` with an untracked file is stopped. - That working tree stands in only for what the commit moves (outline-cli, todoist/outline-cli#193), so `git commit -qam x && git push origin v1` checks the tag's commit. I went a step further than outline-cli: the check follows which branch is checked out when the commit runs, so `git branch c && git commit … && git push origin c` and a push of the old branch after `git switch -c` are checked against what they hold, and `@` counts as HEAD on a detached HEAD. - `git switch -C` and `git checkout -B` are checked against the commit they force the branch to, and `git branch -f`/`-m`/`-c` and `git update-ref` stop a push in the same command like a branch switch (zendesk-cli, todoist/zendesk-cli#282). `git checkout <commit> -- file` is a file restore, not a move; zendesk-cli's version also let `git checkout <branch> --` through, which does switch, so that form still counts as a move here. - `popd` follows `pushd`'s stack (zendesk-cli). - `xargs` long options that take a value (`--max-args`, `--max-procs`, `--replace` and the rest) and `sudo --chdir`/`-D` are parsed (outline-cli). - `gh pr create -H<branch>` is read (outline-cli). - `git push --tags` checks every tag `main` doesn't already contain (outline-cli 8e24cd1), so release tags push freely and a tag on unverified code doesn't. A push that follows tags (`--follow-tags` or `push.followTags`) checks the annotated tags the pushed commits reach that `main` doesn't contain. - A branch the command makes or commits to counts whether the push names it as `publish`, `heads/publish` or `refs/heads/publish`. - An inline `git -c push.*`/`remote.*` setting no longer overrides a refspec in the command (comms-cli, zendesk-cli). - The snapshot fails on a file it can't read (`EACCES`) instead of leaving it out of the fingerprint (comms-cli, outline-cli). I tried it with real agents, each in a throwaway clone pushing to a local bare repository. Claude Code (`claude -p`, Haiku) was denied with the guard's reason on an unverified commit, ran `npm run verify`, and the same push went through; the transcript shows the `PreToolUse` denial and the bare repo has the branch. Codex 0.172 did the same in a trusted plain clone (in a linked worktree it runs the main checkout's `.codex/hooks.json`, as docs/agents.md says). Codex users need to run `codex --trust` in their main checkout and review the new hook in `/hooks` once. ## PR Checklist - [x] Added tests for bugs / new features - [x] Updated docs (README, etc.)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Replaces the end-of-turn gate with a push check, ported from todoist/todoist-cli#672. Before each shell command, the Claude Code and Codex hooks (now one
PreToolUsehook onBash, replacingUserPromptSubmitandStop) call.agents/scripts/guard.ts. It stops an agent'sgit pushorgh pr createfrom this checkout untilnpm run verifyhas passed on exactly the commit being pushed, not the working tree. That's where a person starts reviewing, so that's where the proof has to exist. Pushes a person runs in their own terminal never go through it.The guard reads the shell command without running it:
cd,pushd,git -C,env -Cand the other wrappers, subshells,eval,sh -candenv -Sstrings, refspecs and tags,gh -Rand--head, quotes and here-documents (so a commit message that saysgit pushis ignored). It fingerprints the pushed commit fromgit ls-tree, so the working-tree snapshot now hashes files the way git hashes a blob. What needs no verify follows verify itself: changes it records nothing for (docs, CI-only files), a branch whose code is back tomain's, dry runs, deletions and tag-only pushes. A push whose contents can't be told from the command is stopped. For a commit and a push in one command, it checks the working tree only when the commit takes all of it (everything staged first,git commit -awith nothing untracked, or a plain commit with nothing unstaged), and otherwise stops the push.DISABLE_AGENT_GUARD=1still turns it off, a checkout without the guard is skipped silently, a guard that fails allows the action and says why, and the retiredturn-start/turn-endevents that older hook configs still send are ignored quietly.Before this change the tooling in
.agents/scriptswas the same as todoist-cli's apart from verify's steps and a few comms-specific test rows, so the code started as todoist-cli's as merged, with every fix from its review. The docs keep comms-cli's wording around it. Doistbot's review here found four problems todoist-cli has too, each fixed in its own commit: a branch reset bygit switch -C,git checkout -Borgit branch -fwas checked by its old tip, an inlinegit -c push.*setting made a push with explicit refspecs read as every branch, a file the snapshot couldn't read counted as deleted, and a commit and push in one command was checked against the whole working tree even when the commit takes less.Before those fixes, I tried it with real agents at the top of this stack (this PR, #136 and #137), in a throwaway clone pushing to a local bare repository. Claude Code (
claude -p) and Codex 0.172 (codex exec, in a plain clone once trusted) were each blocked on an unverified commit with the guard's reason, rannpm run verify, and then pushed; the bare repository's refs matched the pushed commits, and the transcripts showed the hook's denial on the first push.Codex users need to run
codex --trustin their main checkout and review the new hook in/hooksonce. In a linked worktree, Codex runs the main checkout's.codex/hooks.json, so a worktree is guarded once the main checkout has this hook.